package com.shen.JUC.Lock8;

import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.locks.Lock;

//自旋锁
public class SpinLock {
    AtomicReference<Thread> reference=new AtomicReference<>();


    //加锁
    public void myLock(){
        Thread thread = Thread.currentThread();
        System.out.println(Thread.currentThread().getName()+"  >lock");
        //自旋锁
        while (!reference.compareAndSet(null,thread));
    }

    //解锁
    public void unlock(){
        Thread thread = Thread.currentThread();
        System.out.println(Thread.currentThread().getName()+"  >un");
        reference.compareAndSet(thread,null);
    }
}
